

import java.util.*;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Scanner;

/**
 * Created by Lenovo on 2017/5/9.
 */
public class Conversion {
    private Stack<String> stack;
    private String string;

    public Conversion() {
        stack = new Stack<String>();
    }

    public String read(String str) {
        String token;
        Scanner scanner = new Scanner(str);
        scanner.useDelimiter("/");
        while (scanner.hasNext()) {
            string = scanner.next();
        }
        return string;
    }

    public String storage(String expr) {
        String token;
        StringTokenizer tokenizer = new StringTokenizer(expr);
        List<String> list = new ArrayList<String>();
        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();
            //判断数字还是符号
            if (isOperator1(token)) {
                //判断栈是否为空或为左括号
                if (isOperator3()) {
                    stack.push(token);
                }
                //与栈顶符号比较
                else if (isOperator4(token)) {
                    while (!stack.empty() && !stack.peek().equals("(")) {
                        String s = stack.pop();
                        list.add(s);
                        list.add(" ");
                    }
                    stack.push(token);
                }
                //判断是否为右括号
                else if (token.equals(")")) {
                    while (!stack.peek().equals("(")) {
                        String s = stack.pop();
                        list.add(s);
                        list.add(" ");
                    }
                    stack.pop();
                }
                //
                else if (isOperator1(token)){
                    while (!stack.empty() && isOperator2()){
                         String s = stack.pop();
                         list.add(s);
                         list.add(" ");
                    }
                    stack.push(token);
                }
                //
                else{
                    stack.push(token);
                }
            }
            //数字直接输出
            else {
                list.add(token);
                list.add(" ");
            }
        }
        //将栈中的剩余元素输出
        while (!isOperator3()) {
            String s = stack.pop();
            list.add(s);
            list.add(" ");
        }
        //将list转成String数组
        String[] str = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            str[i] = list.get(i);
        }
        //将String数组转成String类型
        String c = " ";
        for (int i = 0; i < str.length; i++) {
            c += str[i];
        }
        return c;
    }

    private boolean isOperator1(String token) {
        return (token.equals("+") || token.equals("-") ||
                token.equals("*") || token.equals("/") || token.equals("(") || token.equals(")")
        );
    }

    // 栈顶
    private boolean isOerator1(String token) {
        return (token.equals("*") || token.equals("/"));
    }

    private boolean isOperator2() {
        return ((stack.peek().equals("*")) || stack.peek().equals("/"));
    }

    private boolean isOperator3() {
        return (stack.empty() || stack.peek().equals("("));
    }

    //操作符
    private boolean isOperator4(String token) {
        return (token.equals("+") || token.equals("-"));
    }

    //
    private boolean isOperator5() {
        return (stack.peek().equals("+") || stack.peek().equals("-"));
    }
}
